package com.snake.springboot.authority.web.controller;

import com.snake.springboot.authority.api.model.AuthMenuModel;
import com.snake.springboot.authority.api.model.AuthRoleMenuModel;
import com.snake.springboot.authority.api.response.AuthMenuComplexRsp;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.api.service.AuthMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.snake.common.validate.Create;
import org.snake.common.validate.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类 {@code AuthMenuController}  <br> .
 * <p>权限管理-菜单
 * 创建时间：2024/5/30 17:19
 *
 * @author Yzhui
 * @see
 */

@Api(tags = "权限管理-菜单")
@RestController
@RequestMapping("/auth/menu")
public class AuthMenuController {

    private AuthMenuService authMenuService;

    @Autowired
    public AuthMenuController(AuthMenuService authMenuService) {
        this.authMenuService = authMenuService;
    }

    @ApiOperation("添加菜单")
    @PostMapping("/add")
    public ResponseResult<AuthMenuModel> addAuthMenu(
            @ApiParam(name = "权限菜单入参", value = "传入json格式", required = true)
            @RequestBody @Validated({Create.class}) AuthMenuModel model) throws Exception {
        AuthMenuModel result = authMenuService.addMenu(model);
        return ResponseResultFactory.ucc("添加成功！", result);
    }

    @ApiOperation("修改菜单")
    @PostMapping("/modify")
    public ResponseResult<AuthMenuModel> modifyAuthMenu(
            @ApiParam(name = "权限菜单入参", value = "传入json格式", required = true)
            @RequestBody @Validated({Update.class}) AuthMenuModel model) throws Exception {
        authMenuService.modifyMenu(model);
        return ResponseResultFactory.ucc("修改成功！");
    }

    @ApiOperation("修改菜单排序")
    @PostMapping("/modify-sort")
    public ResponseResult<AuthMenuModel> modifyMenuSort(
            @ApiParam(name = "权限菜单入参", value = "传入json格式", required = true)
            @RequestBody AuthMenuModel model) throws Exception {
        authMenuService.modifyMenuSort(model);
        return ResponseResultFactory.ucc("修改成功！");
    }

    @ApiOperation("修改菜单可用")
    @PostMapping("/modify-enable")
    public ResponseResult<AuthMenuModel> modifyMenuEnable(
            @ApiParam(name = "权限菜单入参", value = "传入json格式", required = true)
            @RequestBody AuthMenuModel model) throws Exception {
        authMenuService.modifyMenuEnable(model);
        return ResponseResultFactory.ucc("修改成功！");
    }



    @ApiOperation("删除菜单")
    @GetMapping("/remove/{menuid}")
    public ResponseResult<AuthMenuModel> removeAuthMenu(
            @ApiParam(name = "权限菜单ID", value = "传入json格式", required = true)
            @PathVariable("menuid") String menuId) {
        authMenuService.removeMenu(menuId);
        return ResponseResultFactory.ucc("删除成功！");
    }

    @ApiOperation("查询菜单详情")
    @GetMapping("/query/{menuid}")
    public ResponseResult<AuthMenuComplexRsp> querySingleAuthMenu(
            @ApiParam(name = "权限菜单ID", value = "传入json格式", required = true)
            @PathVariable("menuid") String menuId) {
        AuthMenuComplexRsp result = authMenuService.querySingleMenu(menuId);
        return ResponseResultFactory.ucc("查询成功！", result);
    }

    @ApiOperation("查询菜单列表")
    @PostMapping("/tree-list")
    public ResponseResult<List<AuthMenuRsp>> queryAuthMenuTreelist(
            @ApiParam(name = "权限菜单树查询", value = "传入json格式", required = true)
            @RequestBody AuthRoleMenuModel params){
        List<AuthMenuRsp> result =  authMenuService.queryMenuTree(params);
        return ResponseResultFactory.ucc("查询成功！", result);
    }

}
